package com.shijie.controller.erp;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shijie.component.ErpUserService;
import com.shijie.core.Result;
import com.shijie.core.ResultGenerator;
import com.shijie.core.ServiceException;
import com.shijie.core.annotation.Log;
import com.shijie.core.annotation.RequiresPermissions;
import com.shijie.core.constants.PnConstant.BusinessType;
import com.shijie.domain.entity.AdminRoles;
import com.shijie.domain.entity.Admins;
import com.shijie.domain.entity.Roles;
import com.shijie.service.AdminRolesService;
import com.shijie.service.AdminsService;
import com.shijie.service.RolesService;

/*
 *  @author: smart boy
 *  @date: 2018-08-06
 */
@RestController
@RequestMapping("/erp/admin")
public class AdminsController {
	@Autowired
	private AdminsService adminsService;
	@Autowired
	private ErpUserService erpUserService;
	@Resource
	private RolesService rolesService;
	@Resource
	private AdminRolesService  adminRolesService;

	@GetMapping("info")
	public Result info() throws ServiceException {
		Admins adminx = adminsService.selectById(erpUserService.getUserId());
		adminx.setMenus(rolesService.getMenuListByUserId(adminx.getId()));
		adminx.setRoles(rolesService.getRolesByUserId(adminx.getId()));
		return ResultGenerator.success(adminx);
	}

	@RequiresPermissions(value={"system:user"})
	@GetMapping
	public Result list(@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer pageSize,@RequestParam(required=false) String q) {
		PageHelper.startPage(page, pageSize);
		Condition condition = new tk.mybatis.mapper.entity.Condition(Admins.class);
		Example.Criteria criteria =  condition.createCriteria();
		if(!StringUtils.isEmpty(q)){
			criteria.andLike("loginName", "%"+q+"%").orLike("userName", "%"+q+"%").orLike("phoneNo", "%"+q+"%");
		}
		criteria.andEqualTo("delFlag", 0);
		List<Admins> list = adminsService.selectByCondition(condition);
		PageInfo<Admins> pageInfo = new PageInfo<>(list);
		return ResultGenerator.success(pageInfo);
	}
	
	@GetMapping("adminRoles")
	public Result adminRole(@RequestParam Integer adminId) throws ServiceException {
		Condition condition = new Condition(AdminRoles.class);
		condition.createCriteria().andEqualTo("userId", adminId);
		List<AdminRoles> adminRoles = adminRolesService.selectByCondition(condition);
		List<Integer> roleIdList = new ArrayList<>();
		adminRoles.forEach(c->{roleIdList.add(c.getRoleId());});
		Condition conditionx = new Condition(Roles.class);
		conditionx.createCriteria().andIn("id", roleIdList);
		List<Roles> roles = rolesService.selectByCondition(conditionx);
		return ResultGenerator.success(roles);
	}

	@RequiresPermissions(value={"system:user:create","system:user:edit"})
	@PostMapping
	@Log(title = "员工管理", action = BusinessType.INSERT)
	public Result add(@RequestBody Admins admins) {
		adminsService.save(admins);
		return ResultGenerator.success();
	}

	@RequiresPermissions(value={"system:user:delete"})
	@Log(title = "员工管理", action = BusinessType.DELETE)
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable Integer id) {
		adminsService.deleteById(id);
		return ResultGenerator.success();
	}

	@GetMapping("/{id}")
	public Result detail(@PathVariable Integer id) {
		Admins admins = adminsService.selectById(id);
		return ResultGenerator.success(admins);
	}
	
	@GetMapping("validate")
    public Result validate(@RequestParam(required = false) Integer id,@RequestParam(required = true) String name) {
        Condition condition =  new Condition(Admins.class);
        Example.Criteria  criteria = condition.createCriteria();
        if(id == null){
        	criteria.andEqualTo("loginName", name);
        }else{
        	criteria.andEqualTo("loginName", name);
        	criteria.andNotEqualTo("id", id);
        }
        List<Admins> list = adminsService.selectByCondition(condition);
        return ResultGenerator.success(list.size() >0?false:true);
	}
}
